package com.cgt.xiaotao.product.controller;

import com.cgt.common.utils.R;
import com.cgt.xiaotao.product.domain.dto.CartDTO;
import com.cgt.xiaotao.product.domain.vo.CartVO;
import com.cgt.xiaotao.product.service.ICartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "购物车接口")
@Slf4j
@RestController
@RequestMapping("/cart")
@RequiredArgsConstructor
public class CartController {

    private final ICartService cartService;

    @ApiOperation("添加进我的购物车")
    @PostMapping("/add")
    public R<String> addCart(@RequestBody CartDTO dto) {
        try {
            cartService.addCart(dto);
            return R.ok("添加成功");
        } catch (Exception e) {
            return R.fail("加入购物车失败");
        }
    }

    @ApiOperation("查询我的购物车")
    @GetMapping("/list")
    public R<List<CartVO>> listCart() {
        try {
            return R.ok(cartService.listCart());
        } catch (Exception e) {
            return R.fail("查询购物车失败");
        }
    }

    @ApiOperation("更新购物车")
    @PutMapping("/update")
    public R<String> updateCart(@RequestBody CartDTO dto) {
        try {
            cartService.updateCart(dto);
            return R.ok("更新成功");
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    @ApiOperation("删除购物车")
    @DeleteMapping("/delete/{productId}")
    public R<String> deleteCart(@PathVariable Long productId) {
        try {
            cartService.deleteCart(productId);
            return R.ok("删除成功");
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }
}
